﻿using INTEX.DataBase;
using System;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{

    public class MVTEXAPPData : BaseFunction
    {
        string xml = @"C:\IntexReport\Data\MVTEXAPP\MVT_EX.XML";
        string exl = @"C:\IntexReport\Data\MVTEXAPP\MVT_EX.xls";
        public SETINTEXDB INTEXDB = new SETINTEXDB();
        public DataSet SetCAD;
        public DataSet SetExt;
     
        public MVTEXAPPData()
            : base()
        {
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        } 
        ~MVTEXAPPData()
        {
            if (exAgent != null)
            {
                exAgent.Close();
                exAgent.Dispose();
                exAgent.KillExcelProcess();
                GC.Collect();
                exAgent = null;
            }
        } 
        private void CheckDirectory()
        {
            if (!System.IO.Directory.Exists(@"C:\IntexReport"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport");
            }
            if (!System.IO.Directory.Exists(@"C:\IntexReport\Data"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport\Data");
            }
            if (!System.IO.Directory.Exists(@"C:\IntexReport\Data\MVTEXAPP"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport\Data\MVTEXAPP");
            }
            if (!System.IO.Directory.Exists(@"C:\IntexReport\Data\MVTEXAPP\01Temp"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport\Data\MVTEXAPP\01Temp");
            }

        }
        public void SaveExcel(DataSet src)
        {
            try
            {
                string ShortName = "MVT_EX.xls";
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent(exl);
                }
                exAgent.SetCurrentSheet("ICC_EXORD");
                int x = 3;
                int y = 1;
                exAgent.DataTableToExcel(src.Tables["ICC_EXORD"], 3, 1);
                exAgent.SetCurrentSheet("ICC_MAXORD");           
                exAgent.DataTableToExcel(src.Tables["ICC_MAXORD"], 3, 1);            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
            finally
            {
                if (exAgent != null)
                {
                    exAgent.Close();
                    exAgent.Dispose();
                    exAgent.KillExcelProcess();
                    GC.Collect();
                    exAgent = null;
                }
            }
        }
        public void Convert2xml()
        {
            DataSet SResult = new DataSet();
            bool IsSuccess = true;
            try
            {
                CheckDirectory();
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent(exl);
                }
                if (System.IO.File.Exists(exl))
                {
                    SResult = new DataSet();
                    SResult = exAgent.LoadDataTableDef(exl, "", 50);
                    Save(SResult);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
            finally
            {
                if (exAgent != null)
                {
                    exAgent.Close();
                    exAgent.Dispose();
                    exAgent.KillExcelProcess();
                    GC.Collect();
                    exAgent = null;
                }
            }
        }
    
 
        public DataSet LoadXML()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            try
            {
                SResult.ReadXml(xml);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
            return SReturn;
        }
        public void Save(DataSet src)
        {
            try
            {
                if (System.IO.File.Exists(xml))
                {
                    System.IO.File.Delete(xml);
                }
                src.WriteXml(xml);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        } 
    }
}
